//require配置

require.config({
  baseUrl: "../javascripts/library",
  paths: {
    jquery: "jquery",
    axios: "axios",
  },
});

require(["jquery", "axios"], function ($, axios) {
  axios
    .get("/product/getItems")
    .then((res) => {
      let data = res.data;
      data.forEach((el) => {
        const pic = JSON.parse(el.picture);
        let temp = `
        <a href="../html/shopinfo.html?id=${el.id}" class="feed-item hover-img" title="${el.product}">
        <div class="item-img">
          <img src="../${pic[0].src}" alt="">
        </div>
        <div class="name">${el.title}</div>
        <div class="price">
          <span class="cur-price">¥${el.nowprice}</span>
        </div>
        <div class="label-line">
          <span class="label">直降</span>
        </div>
      </a>
      `;
        $(".floor:last-of-type .feed-list").append(temp);
      });
    })
    .catch((err) => {
      console.error(err);
    });

  $("body").on("click", function (ev) {
    let username, password;
    if (ev.target == $(".quc-button-list>input")[0]) {
      username = $(".quc-input>input")[0].value;
      password = $(".quc-input>input")[1].value;
      axios
        .get("/users/login", {
          params: {
            username,
            password,
          },
        })
        .then((res) => {
          let data = res.data;
          if (data.token) {
            let token = `${data.token}`;
            localStorage.setItem("token", token);
            alert(`${data.msg}`);
            location.reload();
          } else {
            alert(`${data.msg}`);
            $(".quc-input>input")[1].value = "";
          }
        })
        .catch((err) => {
          console.error(err);
        });
    }
  });
});
